<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <OutputPath>bin</OutputPath>
    <TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
    <TargetOS Condition="'$(TargetOS)' == ''">iossimulator</TargetOS>
    <RuntimeIdentifier>$(TargetOS)-$(TargetArchitecture)</RuntimeIdentifier>
    <DefineConstants Condition="'$(ArchiveTests)' == 'true'">$(DefineConstants);CI_TEST</DefineConstants>
    <AppName>HelloiOS</AppName>
    <MainLibraryFileName>$(AssemblyName).dll</MainLibraryFileName>
    <SelfContained>true</SelfContained>

    <RunAOTCompilation Condition="'$(RunAOTCompilation)' == ''">true</RunAOTCompilation>
    <PublishTrimmed>true</PublishTrimmed>
    <TrimMode>Link</TrimMode>
    <Optimized Condition="'$(Configuration)' == 'Release'">true</Optimized>
    <ShouldILStrip Condition="'$(RunAOTCompilation)' == 'true' and '$(MonoForceInterpreter)' != 'true'">true</ShouldILStrip>

    <EnableDefaultAssembliesToBundle>true</EnableDefaultAssembliesToBundle>
    <AppleGenerateAppBundle>true</AppleGenerateAppBundle>
    <GenerateXcodeProject>true</GenerateXcodeProject>
    <UseConsoleUITemplate>false</UseConsoleUITemplate>

    <BuildAppBundleAfterTargets Condition="'$(ArchiveTests)' != 'true'">CopyFilesToPublishDirectory</BuildAppBundleAfterTargets>
    <BuildAppBundleAfterTargets Condition="'$(ArchiveTests)' == 'true'">CopyFilesToPublishDirectory;Build</BuildAppBundleAfterTargets>
    <BuildAppBundleDependsOnTargets Condition="'$(ArchiveTests)' == 'true'">Publish</BuildAppBundleDependsOnTargets>
  </PropertyGroup>

  <ItemGroup>
    <RuntimeComponents Condition="'$(DiagnosticPorts)' != ''" Include="diagnostics_tracing" />
  </ItemGroup>

  <Import Project="$(MonoProjectRoot)\msbuild\apple\build\AppleBuild.props" />
  <Import Project="$(MonoProjectRoot)\msbuild\apple\build\AppleBuild.InTree.targets" />

  <Target Name="BuildAppBundle" AfterTargets="$(BuildAppBundleAfterTargets)" DependsOnTargets="$(BuildAppBundleDependsOnTargets)"/>
  <Target Name="_SetAppleGenerateAppBundleProps" Condition="'$(TargetOS)' != 'ios' and '$(ArchiveTests)' != 'true'" BeforeTargets="_AppleGenerateAppBundle">
    <ItemGroup>
      <RuntimeComponents Include="diagnostics_tracing" KeepDuplicates="false" />
    </ItemGroup>
  </Target>

  <Target Name="RunAppBundle"
          AfterTargets="_AppleGenerateAppBundle"
          Condition="'$(ArchiveTests)' != 'true' and '$(DeployAndRun)' == 'true'">
    <!-- Install and run on a device or simulator -->
    <Exec Condition="'$(TargetOS)' == 'iossimulator'" Command="dotnet xharness apple run --app=$(AppBundlePath) --targets=ios-simulator-64 --output-directory=/tmp/out" />
    <Exec Condition="'$(TargetOS)' == 'ios'" Command="dotnet xharness apple run --app=$(AppBundlePath) --targets=ios-device --output-directory=/tmp/out" />

    <!-- run on maccatalyst -->
    <Exec Condition="'$(TargetOS)' == 'maccatalyst'" Command="open -W $(AppBundlePath)" />
  </Target>

  <Target Name="CopySampleAppToHelixTestDir"
          Condition="'$(ArchiveTests)' == 'true'"
          AfterTargets="_AppleGenerateAppBundle" >
    <PropertyGroup>
      <!-- Helix properties -->
      <!-- AnyCPU as Platform-->
      <OSPlatformConfig>$(TargetOS).AnyCPU.$(Configuration)</OSPlatformConfig>
      <!-- <OSPlatformConfig>$(TargetOS).$(Platform).$(Configuration)</OSPlatformConfig> -->
      <HelixArchiveRoot>$(ArtifactsDir)helix/</HelixArchiveRoot>
      <HelixArchiveRunOnlyRoot>$(HelixArchiveRoot)runonly/</HelixArchiveRunOnlyRoot>
      <HelixArchiveRunOnlyAppsDir>$(HelixArchiveRunOnlyRoot)$(OSPlatformConfig)/</HelixArchiveRunOnlyAppsDir>
      <_AppBundleName>$([System.IO.Path]::GetFileName('$(AppBundlePath)'))</_AppBundleName>
    </PropertyGroup>
    <ItemGroup>
      <_appFiles Include="$(AppBundlePath)/**/*" />
    </ItemGroup>
    <Copy SourceFiles="@(_appFiles)"
          DestinationFolder="$(HelixArchiveRunOnlyAppsDir)/$(_AppBundleName)/%(RecursiveDir)" />

    <Message Importance="High" Text="AppBundlePath: $(AppBundlePath)"/>
    <Message Importance="High" Text="HelixArchiveRunOnlyAppsDir: $(HelixArchiveRunOnlyAppsDir)"/>
  </Target>
</Project>
